package com.fzb.forum4j.controller;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.stereotype.Controller;

import com.fzb.forum4j.po.Article;
import com.fzb.forum4j.po.Category;
import com.fzb.forum4j.po.QueryMode;
import com.fzb.forum4j.po.RequestRecord;
import com.fzb.forum4j.po.WebSite;
import com.fzb.forum4j.service.api.IArticleService;
import com.fzb.forum4j.service.api.IWebSiteService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("webSiteAction")
public class WebSiteAction extends ActionSupport implements ServletContextAware{
	/**
	 * 网站配置管理
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name="articleServiceImpl")
	private IArticleService articleService;
	
	private ServletContext context;
	
	private String date;
	
	private Set<RequestRecord> requestRecodes;
	
	@Resource(name="webSiteServiceImpl")
	private IWebSiteService service;
	public String init(){
 		WebSite webSite=(WebSite) context.getAttribute("webSite");
		if(true){
			QueryMode<Article> qmd=new QueryMode<Article>();
			QueryMode<WebSite> qmdw=new QueryMode<WebSite>();
			WebSite web=new WebSite();
			qmdw.setT(web);
			qmdw.setId(1);
			service.getEntityByEid(qmdw);
			context.setAttribute("webSite", qmdw.getData().get("data"));
			//qmd.setRows(((WebSite)qmdw.getData().get("data")).g);
			Set<Category> categories=new HashSet<Category>();
			Category category=new Category();
			category.setCatalog("question");
			categories.add(category);
			category=new Category();
			category.setCatalog("news");
			categories.add(category);
			qmd.setT(new Article());
			articleService.getInitData(qmd,categories);
			ActionContext.getContext().put("data", qmd.getData());
			System.out.println(ActionContext.getContext().get("data"));
		}
		ActionContext.getContext().put("webSite", webSite);
		return SUCCESS;
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context=context;
	}
	
	
	public String accessRecords(){
		Map<String,Set<RequestRecord>> accessRecords=(Map<String, Set<RequestRecord>>) ServletActionContext.getServletContext().getAttribute("accessRecords");
		if(accessRecords.get(date)!=null){
			requestRecodes=accessRecords.get(date);
		}
		ActionContext.getContext().getValueStack().push(accessRecords);
		return "accessRecords";
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDate() {
		return date;
	}


	 
	 
}
